package com.ruoyi.project.bms.bmsVoucherAssociate.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.bms.bmsVoucherAssociate.domain.BmsVoucherModel;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.bms.bmsVoucherAssociate.mapper.BmsVoucherAssociateMapper;
import com.ruoyi.project.bms.bmsVoucherAssociate.domain.BmsVoucherAssociate;
import com.ruoyi.project.bms.bmsVoucherAssociate.service.IBmsVoucherAssociateService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 登记批次Service业务层处理
 *
 * @author ruoyi
 * @date 2020-03-27
 */
@Service
public class BmsVoucherAssociateServiceImpl extends ServiceImpl<BmsVoucherAssociateMapper, BmsVoucherAssociate> implements IBmsVoucherAssociateService
{
    @Autowired
    private BmsVoucherAssociateMapper bmsVoucherAssociateMapper;

    /**
     * 查询登记批次
     *
     * @param id 登记批次ID
     * @return 登记批次
     */
    @Override
    public BmsVoucherAssociate selectBmsVoucherAssociateById(String id)
    {
        return bmsVoucherAssociateMapper.selectBmsVoucherAssociateById(id);
    }

    /**
     * 查询登记批次列表
     *
     * @param bmsVoucherAssociate 登记批次
     * @return 登记批次
     */
    @Override
    public List<BmsVoucherAssociate> selectBmsVoucherAssociateList(BmsVoucherAssociate bmsVoucherAssociate)
    {
        return bmsVoucherAssociateMapper.selectBmsVoucherAssociateList(bmsVoucherAssociate);
    }

    /**
     * 新增登记批次
     *
     * @param bmsVoucherAssociate 登记批次
     * @return 结果
     */
    @Override
    public int insertBmsVoucherAssociate(BmsVoucherAssociate bmsVoucherAssociate)
    {
        return bmsVoucherAssociateMapper.insertBmsVoucherAssociate(bmsVoucherAssociate);
    }

    /**
     * 修改登记批次
     *
     * @param bmsVoucherAssociate 登记批次
     * @return 结果
     */
    @Override
    public int updateBmsVoucherAssociate(BmsVoucherAssociate bmsVoucherAssociate)
    {
        return bmsVoucherAssociateMapper.updateBmsVoucherAssociate(bmsVoucherAssociate);
    }

    /**
     * 删除登记批次对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBmsVoucherAssociateByIds(String ids)
    {
        return bmsVoucherAssociateMapper.deleteBmsVoucherAssociateByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除登记批次信息
     *
     * @param id 登记批次ID
     * @return 结果
     */
    @Override
    public int deleteBmsVoucherAssociateById(String id)
    {
        return bmsVoucherAssociateMapper.deleteBmsVoucherAssociateById(id);
    }

    /**
     * 根据List<BmsVoucherModel>保存数据
     * @return 结果
     */
    @Override
    public boolean saveBmsVoucherAssociates(User user,List<BmsVoucherModel> list,Integer be)
    {
        List<BmsVoucherAssociate> bmsVoucherAssociates = new ArrayList<BmsVoucherAssociate>();
        for(int i=0;i<list.size();i++){

            BmsVoucherModel bmsVoucherModel = list.get(i);
            if(bmsVoucherModel.getNoteNum()!=0){//只保存登记有数量的
                BmsVoucherAssociate bmsVoucherAssociate = new BmsVoucherAssociate();
                bmsVoucherAssociate.setTltCode(bmsVoucherModel.getTellerCode()+bmsVoucherModel.getOrgCode());
                bmsVoucherAssociate.setNoteCount(bmsVoucherModel.getNoteNum());
                bmsVoucherAssociate.setTrDate(bmsVoucherModel.getTrDate());
                bmsVoucherAssociate.setOrgCode(bmsVoucherModel.getOrgCode());
                bmsVoucherAssociate.setSubmitTltId(user.getUserId());
                bmsVoucherAssociate.setSubmitTime(new java.sql.Date(System.currentTimeMillis()));
                bmsVoucherAssociate.setSubmitStatus(1);
                bmsVoucherAssociate.setTellerCode(bmsVoucherModel.getTellerCode());
                bmsVoucherAssociate.setAuditOrgCode(bmsVoucherModel.getAuOrgCode());
                bmsVoucherAssociate.setBe(be);
                bmsVoucherAssociates.add(bmsVoucherAssociate);
            }
        }
        return this.saveBatch(bmsVoucherAssociates);
        //return bmsVoucherAssociateMapper.deleteBmsVoucherAssociateById(id);
    }
}
